package com.uhu.code;

import com.uhu.code.help.ListNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2. 两数相加
 * https://leetcode.cn/problems/add-two-numbers/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        List<Integer> numbers = new ArrayList<>();
        int more = 0;
        while(l1 != null || l2 != null) {
            int now = more;
            if(l1 != null) {
                now += l1.val;
                l1 = l1.next;
            }
            if(l2 != null) {
                now += l2.val;
                l2 = l2.next;
            }
            if(now >= 10) {
                more = 1;
                now = now%10;
            }else {
                more = 0;
            }
            numbers.add(now);
        }
        if(more == 1) {
            numbers.add(1);
        }
        ListNode result = new ListNode(numbers.get(0));
        ListNode temp = result;
        for(int i = 1; i < numbers.size(); i++) {
            temp.next = new ListNode(numbers.get(i));
            temp = temp.next;
        }
        return result;
    }
}
